package com.ygs.secs.service.impl;

import com.ygs.secs.mapper.EnterpriseMapper;
import com.ygs.secs.model.Enterprise;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.ygs.secs.model.AuthInfomation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ygs.secs.mapper.AuthInfomationMapper;
import com.ygs.secs.service.AuthInfomationService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class AuthInfomationServiceImpl implements AuthInfomationService {

    @Resource
    private AuthInfomationMapper authInfomationMapper;
    @Resource
    EnterpriseMapper enterpriseMapper;

    @Override
    public int deleteByPrimaryKey(Integer authid) {
        return authInfomationMapper.deleteByPrimaryKey(authid);
    }

    @Override
    public int insert(AuthInfomation record) {
        return authInfomationMapper.insert(record);
    }

    @Override
    public int insertSelective(AuthInfomation record) {
        return authInfomationMapper.insertSelective(record);
    }

    @Override
    public AuthInfomation selectByPrimaryKey(Integer authid) {
        return authInfomationMapper.selectByPrimaryKey(authid);
    }

    @Override
    public int updateByPrimaryKeySelective(AuthInfomation record) {
        return authInfomationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AuthInfomation record) {
        return authInfomationMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<AuthInfomation> list) {
        return authInfomationMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<AuthInfomation> list) {
        return authInfomationMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<AuthInfomation> list) {
        return authInfomationMapper.batchInsert(list);
    }

    @Override
    public AuthInfomation getauthinfo(String enterpriseid) {
        return authInfomationMapper.getauthinfo(enterpriseid);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Map<String, Object> odateauthinfo(AuthInfomation authInfomation) {
        Map<String, Object> map = new HashMap<>();
        try {
            AuthInfomation dauinfo = authInfomationMapper.getauthinfo(authInfomation.getEnterprise().getEnterpriseid());
            if (dauinfo != null) {
                authInfomation.setAuthid(dauinfo.getAuthid());
                int i = authInfomationMapper.updateByPrimaryKeySelective(authInfomation);
                Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(authInfomation.getEnterprise().getEnterpriseid());
                enterprise.setCertificationstatus("1");
                int j = enterpriseMapper.updateByPrimaryKeySelective(enterprise);
                if (i > 0 && j > 0) {
                    map.put("code", 200);
                    map.put("msg", "认证信息更新成功");
                } else {
                    map.put("code", 501);
                    map.put("msg", "认证信息更新失败");
                }
            } else {
                Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(authInfomation.getEnterprise().getEnterpriseid());
                enterprise.setCertificationstatus("1");
                int i = authInfomationMapper.insertSelective(authInfomation);
                int j = enterpriseMapper.updateByPrimaryKeySelective(enterprise);
                if (i > 0 && j > 0) {
                    map.put("code", 200);
                    map.put("msg", "认证信息添加成功，耐心等待审核");
                } else {
                    map.put("code", 501);
                    map.put("msg", "认证信息添加失败");
                }
            }
        } catch (Exception e) {
            //防止声明式事务捕获异常不会滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

}


